<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Kuaiyong
 *
 * @author sunday add 2014.5.28  modify by dragon.hu @2016.2.19
 */
require_once 'BaseChannel.php';

class Kuaiyong implements BaseChannel {
	function __construct() {
		//echo "construct";
	}

	//put your code here
	function android($data) {
		//签名密钥
		//$SIGN_PRIVATE_KEY = "u9N2KuQ2YumfTTWvZoBmwa1FdULMSPNn";
		$SIGN_PRIVATE_KEY = "";
		//支付公钥
		//$PAYMENT_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDQJ/hDOujbw26T/pGTEHRclU+F/whkISyXoJj59PnEqpicZUFEtYFpu4C2nsszfC9kxV7gLtxxTZmlG3wh4ZV+mU4Tqkvznb34BEyNlzV97+vxAVajEFpdF0QBZ65t8PzPsleXo7lr6EvapWPPNuzfJIxUIxyKJFbFefv2ueKAZQIDAQAB";
		$PAYMENT_PUBLIC_KEY = "";
		$ret = $this->verify($PAYMENT_PUBLIC_KEY, $SIGN_PRIVATE_KEY, ANDROID);
		echo $ret;
		// $this->verify($_POST['sign'],$_POST['notify_data'],$_POST['orderid'],$_POST['dealseq'],$_POST['uid'],$_POST['subject'],$_POST['v'],$orderLog,$PAYMENT_PUBLIC_KEY,"android");
	}

	function ios($data) {
		//签名密钥
		//$SIGN_PRIVATE_KEY = "u9N2KuQ2YumfTTWvZoBmwa1FdULMSPNn";
		$SIGN_PRIVATE_KEY = "392cd9d7ac596d3e3148874dcb86e8b2";  //签名秘钥
		//支付公钥
		//$PAYMENT_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDQJ/hDOujbw26T/pGTEHRclU+F/whkISyXoJj59PnEqpicZUFEtYFpu4C2nsszfC9kxV7gLtxxTZmlG3wh4ZV+mU4Tqkvznb34BEyNlzV97+vxAVajEFpdF0QBZ65t8PzPsleXo7lr6EvapWPPNuzfJIxUIxyKJFbFefv2ueKAZQIDAQAB";
		$PAYMENT_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCffdgwWrpo4AIZXFUnxy/vL2jGvBx2ePnFaJCdumuClrcbyhS++iyW0oeG/8gOoHZFLhmOVgZXUswESGYLFvBcLIxZtB4LqgWY2oPBj62/oiDrDxNv8lb0rSfg9aG3+C7xJ+qXlQYTJJTMjpLvbesaZn4RLRdQk06DDrDVSwC14QIDAQAB";  //RSA公钥
		$ret = $this->verify($PAYMENT_PUBLIC_KEY, $SIGN_PRIVATE_KEY, IOS);
		echo $ret;
	}

	public function verify($PAYMENT_PUBLIC_KEY, $SIGN_PRIVATE_KEY, $platfrom) 
	{
		include("include/kuaiyong/Rsa.php"); //快用使用到的rsa加密算法
		//记录日志等数据
		$orderLog = new ChargeOrder();
		$orderLog->log("kuaiyong",$_REQUEST);

		//RSA 签名。签名的原串，是将收到，除去 sign 这个值之外的参数，根据key 值做排序组成的 url 参数形式，形如右面的示例。
		$post_sign = $_POST['sign'];
		//加密的关键数据，解密后格式为： dealseq=20130219160809567&fee=0 .01&payresult=0；其中 payresult 是支付结果：0：成功，-1：支付失败，
		$post_notify_data = $_POST['notify_data'];
		$post_orderid = $_POST['orderid'];	//快用平台订单号
		$post_dealseq = $_POST['dealseq'];	//游戏订单号（透传，唯一开发商可以自定义的参数）
		$post_uid = $_POST['uid'];	//游戏帐号,在 SDK 提交此参数时，必须传入快用用户系统返回的guid，开发商不要做修改
		$post_subject = $_POST['subject'];	//购买物品
		$post_v = $_POST['v'];	//版本号
		
		//print_r($_POST);
		
		if ($post_sign == "") 
		{
			echo " order:$post_dealseq post_sign is null ";
			return "failed";
		}

		//把公钥的格式转换下
		//$publicKey = $this->PAYMENT_PUBLIC_KEY;
		$publicKey = $PAYMENT_PUBLIC_KEY;
		$post_sign = base64_decode($post_sign);

		//对输入参数根据参数名排序，并拼接为key=value&key=value格式；
		$parametersArray = array();

		$parametersArray['notify_data'] = $post_notify_data;
		$parametersArray['orderid'] = $post_orderid;
		$parametersArray['dealseq'] = $post_dealseq;
		$parametersArray['uid'] = $post_uid;
		$parametersArray['subject'] = $post_subject;
		$parametersArray['v'] = $post_v;

		//对参数按英文大小写排序 拼接
		ksort($parametersArray);
		//print_r($parametersArray);
		$sourcestr = "";
		
		foreach ($parametersArray as $key => $val) 
		{
			$sourcestr == "" ? ($sourcestr = "$key=$val") : ($sourcestr .="&$key=$val");
		}
		//echo " sourcestr1:$sourcestr \r\n";
		
		//第二种方法，自动排序
		//for test
		$sourcestr="dealseq=".$post_dealseq."&notify_data=$post_notify_data&orderid=$post_orderid&subject=$post_subject"
					."&uid=$post_uid&v=$post_v";
		//echo " sourcestr2:$sourcestr \r\n";
		//对数据进行验签，注意对公钥做格式转换
		$publicKey = Rsa::instance()->convert_publicKey($publicKey);

		$verify = Rsa::instance()->verify($sourcestr, $post_sign, $publicKey);

		//echo " verify:$verify \r\n";
		//verify不是1，就是不合法的订单
		if ($verify != 1) 
		{
			echo (" order:$post_dealseq Failed to verify data");
			return "failed";
		}

		//对加密的notify_data进行解密
		$post_notify_data_decode = base64_decode($post_notify_data);

		$decode_notify_data = Rsa::instance()->publickey_decodeing($post_notify_data_decode, $publicKey);

		//把解密后的字符解析成数组,结果保存在resultArray中
		parse_str($decode_notify_data, $resultArray);

		$orderLog->log("kuaiyong", array("orderid" => $post_dealseq, "fee" => $resultArray['fee'], "payresult" =>$resultArray['payresult']));

		//比较解密出的数据中的dealseq和参数中的dealseq是否一致
		if ($resultArray['dealseq'] == $post_dealseq) 
		{
			//TODO：开发商根据dealseq将支付结果记录下来，并根据支付结果做相应处理
			//记录当前的结果 0：成功，-1：支付失败，-2 超时失败；
			if (0 == $resultArray['payresult']) 
			{
				$status = STATUS_SUCCESS; //1表示 支付成功
			} else 
			{
				$status = STATUS_FAILD; //2表示 支付失败
			}

			if($orderLog->recordOrder($status, $resultArray['fee'], $post_dealseq, "IosKuaiYong", $platfrom,$post_orderid))
			{
				$orderLog->log("kuaiyong", "status:$status money:".$resultArray['fee']." order:$post_dealseq success ");
				return "success";
			}
			else
			{
				$orderLog->log("kuaiyong", "status:$status money:".$resultArray['fee']." order:$post_dealseq insert database error ");
				return "failed";
			}
		} else 
		{
			echo ( " order:$post_dealseq == ".$resultArray['dealseq']." did not match");
			return "failed";
		}
	}

}

?>
